package com.project.schoolFoodShare.controller.comment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.config.utils.ShiroUtils;
import com.project.schoolFoodShare.domain.FoodComment;
import com.project.schoolFoodShare.domain.FoodList;
import com.project.schoolFoodShare.domain.Result;
import com.project.schoolFoodShare.domain.User;
import com.project.schoolFoodShare.service.FoodCommentService;
import com.project.schoolFoodShare.service.FoodListService;
import com.project.schoolFoodShare.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description: 评论controller
 */
@Controller
@ResponseBody
@RequestMapping("comment")
public class FoodCommentController {

    @Autowired
    private FoodCommentService foodCommentService;
    @Autowired
    private UserService userService;
    @Autowired
    private FoodListService foodListService;

    /** 分页获取评论 */
    @PostMapping("getFoodCommentPage")
    public Result getFoodCommentPage(@RequestBody FoodComment foodComment) {
        Page<FoodComment> page = new Page<>(foodComment.getPageNumber(),foodComment.getPageSize());
        QueryWrapper<FoodComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(foodComment.getUserId()),FoodComment::getUserId,foodComment.getUserId())
                .eq(StringUtils.isNotBlank(foodComment.getListId()),FoodComment::getListId,foodComment.getListId())
                .like(StringUtils.isNotBlank(foodComment.getContent()),FoodComment::getContent,foodComment.getContent())
                .eq(StringUtils.isNotBlank(foodComment.getImage()),FoodComment::getImage,foodComment.getImage())
                .eq(StringUtils.isNotBlank(foodComment.getReplyId()),FoodComment::getReplyId,foodComment.getReplyId())
                .like(StringUtils.isNotBlank(foodComment.getCreateBy()),FoodComment::getCreateBy,foodComment.getCreateBy())
                .eq(foodComment.getCreateTime() != null,FoodComment::getCreateTime,foodComment.getCreateTime())
                .eq(StringUtils.isNotBlank(foodComment.getUpdateBy()),FoodComment::getUpdateBy,foodComment.getUpdateBy())
                .eq(foodComment.getUpdateTime() != null,FoodComment::getUpdateTime,foodComment.getUpdateTime())
                .orderByDesc(FoodComment::getCreateTime);
        Page<FoodComment> foodCommentPage = foodCommentService.page(page, queryWrapper);
        for (FoodComment comment : foodCommentPage.getRecords()) {
            User user = userService.getById(comment.getUserId());
            comment.setAvatar(user.getAvatar());
            comment.setCreateBy(user.getUserName());
            if (StringUtils.isNotBlank(comment.getReplyId())) {
                FoodComment replay = foodCommentService.getById(comment.getReplyId());
                comment.setReplayName(replay.getCreateBy());
            }
        }
        return Result.success(foodCommentPage);
    }

    /** 根据id获取评论 */
    @GetMapping("getFoodCommentById")
    public Result getFoodCommentById(@RequestParam("id")String id) {
        FoodComment foodComment = foodCommentService.getById(id);
        return Result.success(foodComment);
    }

    /** 保存评论 */
    @PostMapping("saveFoodComment")
    @Transactional(rollbackFor = Exception.class)
    public Result saveFoodComment(@RequestBody FoodComment foodComment) {
        User userInfo = ShiroUtils.getUserInfo();
        foodComment.setUserId(userInfo.getId());
        FoodList foodList = foodListService.getById(foodComment.getListId());
        foodList.setComment(foodList.getComment() + 1);
        foodListService.updateById(foodList);
        boolean save = foodCommentService.save(foodComment);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑评论 */
    @PostMapping("editFoodComment")
    public Result editFoodComment(@RequestBody FoodComment foodComment) {
        boolean save = foodCommentService.updateById(foodComment);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除评论 */
    @GetMapping("removeFoodComment")
    @Transactional(rollbackFor = Exception.class)
    public Result removeFoodComment(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                FoodComment foodComment = foodCommentService.getById(id);
                FoodList foodList = foodListService.getById(foodComment.getListId());
                foodList.setComment(foodList.getComment() - 1);
                foodListService.updateById(foodList);
                foodCommentService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("评论id不能为空！");
        }
    }

}